package com.chen.Sign.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.Sign.common.Result;
import com.chen.Sign.mapper.BlogMapper;
import com.chen.Sign.pojo.*;
import com.chen.Sign.service.impl.BlogServiceImpl;
import com.chen.Sign.service.impl.Blog_imagesServiceImpl;
import com.chen.Sign.service.impl.Blog_tagServiceImpl;
import com.chen.Sign.service.impl.UserServiceImpl;
import com.chen.Sign.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 博客主模块
@RestController
@Slf4j
@SuppressWarnings({"all"})
@RequestMapping("/blog")
@CrossOrigin
public class BlogController {

    @Autowired
    BlogServiceImpl blogService;

    @Autowired
    Blog_tagServiceImpl blog_tagService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    BlogMapper blogMapper;

    @Autowired
    Blog_imagesServiceImpl blog_imagesService;

    // 添加博客
    @PostMapping("/add")
    public Result addBlog(HttpServletRequest request) throws ParseException {
        Result result = new Result();
        // 读取前端传回的博客数据
        String token = request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        Integer id = tokenInfo.getClaim("id").asInt();
        String username = tokenInfo.getClaim("username").asString();
        User user = userService.selectallinformation(username);
        String nickname = user.getNickname();
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String outline = request.getParameter("outline");
        Integer views = 0;/*Integer.valueOf(request.getParameter("views"));*/
        // 创建实体类
        Blog blog = new Blog();
        blog.setUsername(username);
        blog.setNickname(nickname);
        blog.setTitle(title);
        blog.setContent(content);
        blog.setOutline(outline);
        blog.setViews(views);
        boolean flag = blogService.writerBlog(blog);
        Map<String, String> map = new HashMap<>();
        if (flag) {
            return result.insertBlogok(map);
        } else {
            return result.insertBlogno(map);
        }
    }

    // 查看博客
    @GetMapping({"/see/{id}"})
    public Result getById(@PathVariable("id") Integer id) {
        Result result = new Result();
        // 获取博客主要内容
        Blog blog = blogService.getById(id);
        // 获取博客标签
        Blog_tag blog_tag = blog_tagService.getById(id);
        String tag = blog_tag.getTag();
        // 获取博客图片
        Blog_images blog_images = blog_imagesService.getById(id);
        String imagesaddress = blog_images.getImagesaddress();
        // 获取访问量
        Integer views = blog.getViews();
        // 访问量
        ++views;
        // 修改数据库中的访问量数据
        blogService.updateviews(views, id);
        Map<String, String> map = new HashMap<>();
        map.put("nickname", blog.getNickname());
        map.put("title", blog.getTitle());
        map.put("content", blog.getContent());
        map.put("outline", blog.getOutline());
        map.put("views", String.valueOf(views));
        map.put("create_time", String.valueOf(blog.getCreate_time()));
        map.put("tag", tag);
        map.put("gttu", String.valueOf(blog.getGttu()));
        map.put("imagesaddress", imagesaddress);
        return result.seeblogok(map);
    }

    // 修改博客
    @PutMapping("/modify/{id}")
    public Result modify(HttpServletRequest request, @PathVariable("id") Integer id) {
        Result result = new Result();
        Blog blog = blogService.getById(id);
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String outline = request.getParameter("outline");
        blog.setTitle(title);
        blog.setContent(content);
        blog.setOutline(outline);
        blogService.updateById(blog);
        return result.modifyok("");
    }

    // 删除博客
    @DeleteMapping({"/delete/{id}"})
    public Result delete(HttpServletRequest request, @PathVariable("id") Integer id) {
        Result result = new Result();
        blogMapper.deleteById(id);
        return result.deleteok("");
    }

    // 博客入口页--分页显示所有博客的标题和简介
    @GetMapping("/getallblog")
    public Result getallblog(HttpServletRequest request) {
        String current = request.getParameter("current");
        String size = request.getParameter("size");
        String token = request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        Integer id = tokenInfo.getClaim("id").asInt();
        Page<Blog> page = new Page<>(Integer.parseInt(current), Integer.parseInt(size));
        blogMapper.selectPageVo((org.springframework.data.domain.Page<Blog>) page);
        HashMap<String, Object> map = new HashMap<>();
        // 获得数据
        List<Blog> list = page.getRecords();
        map.put("datalist", list);
        // 总记录数
        map.put("total", page.getTotal());
        // 总页数
        map.put("pages", page.getPages());
        // 当前页
        map.put("currentpage", page.getCurrent());
        return new Result(1, "查看数据成功", map);
    }

    @GetMapping("/myallblog")
    public Result myallblog(HttpServletRequest request) {
        String current = request.getParameter("current");
        String size = request.getParameter("size");
        String token = request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        Integer id = tokenInfo.getClaim("id").asInt();
        String username = tokenInfo.getClaim("username").asString();
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<Blog> blogs = blogMapper.selectList(queryWrapper);
        Page<Blog> page = new Page<>(Integer.parseInt(current), Integer.parseInt(size));
        blogMapper.selectPage(page,queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        // 获得数据
        List<Blog> list = page.getRecords();
        map.put("datalist", list);
        // 总记录数
        map.put("total", page.getTotal());
        // 总页数
        map.put("pages", page.getPages());
        // 当前页
        map.put("currentpage", page.getCurrent());
        return new Result(1, "查看数据成功", map);
    }
}
